<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        .register-container {
            background: white;
            padding: 40px;
            border-radius: 10px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 450px;
        }
        
        .register-header {
            text-align: center;
            margin-bottom: 30px;
        }
        
        .register-header h1 {
            color: #333;
            font-size: 28px;
            margin-bottom: 10px;
        }
        
        .register-header p {
            color: #666;
            font-size: 14px;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #333;
            font-weight: 500;
        }
        
        .form-group input {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e1e5e9;
            border-radius: 5px;
            font-size: 16px;
            transition: border-color 0.3s ease;
        }
        
        .form-group input:focus {
            outline: none;
            border-color: #667eea;
        }
        
        .form-group input.error {
            border-color: #dc3545;
        }
        
        .form-group input.success {
            border-color: #28a745;
        }
        
        .password-requirements {
            font-size: 12px;
            color: #666;
            margin-top: 5px;
        }
        
        .register-btn {
            width: 100%;
            padding: 12px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: transform 0.2s ease;
        }
        
        .register-btn:hover {
            transform: translateY(-2px);
        }
        
        .register-btn:disabled {
            background: #ccc;
            cursor: not-allowed;
            transform: none;
        }
        
        .links {
            text-align: center;
            margin-top: 20px;
        }
        
        .links a {
            color: #667eea;
            text-decoration: none;
            margin: 0 10px;
            font-size: 14px;
        }
        
        .links a:hover {
            text-decoration: underline;
        }
        
        .error-message {
            color: #dc3545;
            text-align: center;
            margin-top: 10px;
            font-size: 14px;
        }
        
        .success-message {
            color: #28a745;
            text-align: center;
            margin-top: 10px;
            font-size: 14px;
        }
        
        .validation-message {
            font-size: 12px;
            margin-top: 5px;
        }
        
        .validation-message.error {
            color: #dc3545;
        }
        
        .validation-message.success {
            color: #28a745;
        }
    </style>
</head>
<body>
    <div class="register-container">
        <div class="register-header">
            <h1>用户注册</h1>
            <p>创建您的账户以开始使用待办事项管理系统</p>
        </div>
        
        <form id="registerForm">
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" name="username" required>
                <div class="validation-message" id="usernameValidation"></div>
            </div>
            
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" required>
                <div class="password-requirements">
                    密码至少包含6个字符
                </div>
                <div class="validation-message" id="passwordValidation"></div>
            </div>
            
            <div class="form-group">
                <label for="confirmPassword">确认密码</label>
                <input type="password" id="confirmPassword" name="confirmPassword" required>
                <div class="validation-message" id="confirmPasswordValidation"></div>
            </div>
            
            <button type="submit" class="register-btn" id="submitBtn">注册</button>
        </form>
        
        <div class="links">
            <a href="login.html">已有账户？立即登录</a>
            <a href="index.html">返回首页</a>
        </div>
        
        <div id="message"></div>
    </div>

    <script>
        const username = document.getElementById('username');
        const password = document.getElementById('password');
        const confirmPassword = document.getElementById('confirmPassword');
        const submitBtn = document.getElementById('submitBtn');
        const messageDiv = document.getElementById('message');
        
        // 实时验证用户名
        username.addEventListener('input', function() {
            const value = this.value.trim();
            const validation = document.getElementById('usernameValidation');
            
            if (value.length === 0) {
                this.classList.remove('success', 'error');
                validation.textContent = '';
                validation.className = 'validation-message';
            } else if (value.length < 3) {
                this.classList.remove('success');
                this.classList.add('error');
                validation.textContent = '用户名至少需要3个字符';
                validation.className = 'validation-message error';
            } else {
                this.classList.remove('error');
                this.classList.add('success');
                validation.textContent = '用户名格式正确';
                validation.className = 'validation-message success';
            }
            validateForm();
        });
        
        // 实时验证密码
        password.addEventListener('input', function() {
            const value = this.value;
            const validation = document.getElementById('passwordValidation');
            
            if (value.length === 0) {
                this.classList.remove('success', 'error');
                validation.textContent = '';
                validation.className = 'validation-message';
            } else if (value.length < 6) {
                this.classList.remove('success');
                this.classList.add('error');
                validation.textContent = '密码至少需要6个字符';
                validation.className = 'validation-message error';
            } else {
                this.classList.remove('error');
                this.classList.add('success');
                validation.textContent = '密码格式正确';
                validation.className = 'validation-message success';
            }
            validateForm();
        });
        
        // 实时验证确认密码
        confirmPassword.addEventListener('input', function() {
            const value = this.value;
            const passwordValue = password.value;
            const validation = document.getElementById('confirmPasswordValidation');
            
            if (value.length === 0) {
                this.classList.remove('success', 'error');
                validation.textContent = '';
                validation.className = 'validation-message';
            } else if (value !== passwordValue) {
                this.classList.remove('success');
                this.classList.add('error');
                validation.textContent = '两次输入的密码不一致';
                validation.className = 'validation-message error';
            } else {
                this.classList.remove('error');
                this.classList.add('success');
                validation.textContent = '密码确认正确';
                validation.className = 'validation-message success';
            }
            validateForm();
        });
        
        // 验证整个表单
        function validateForm() {
            const usernameValid = username.classList.contains('success');
            const passwordValid = password.classList.contains('success');
            const confirmPasswordValid = confirmPassword.classList.contains('success');
            
            if (usernameValid && passwordValid && confirmPasswordValid) {
                submitBtn.disabled = false;
            } else {
                submitBtn.disabled = true;
            }
        }
        
        // 表单提交
        document.getElementById('registerForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const usernameValue = username.value.trim();
            const passwordValue = password.value;
            
            try {
                const response = await fetch('/api/user/register', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        username: usernameValue,
                        passwordHash: passwordValue
                    })
                });
                
                if (response.ok) {
                    messageDiv.innerHTML = '<div class="success-message">注册成功！正在跳转到登录页面...</div>';
                    setTimeout(() => {
                        window.location.href = 'login.html';
                    }, 2000);
                } else {
                    const errorData = await response.json();
                    messageDiv.innerHTML = `<div class="error-message">注册失败：${errorData.message || '用户名已存在'}</div>`;
                }
            } catch (error) {
                messageDiv.innerHTML = '<div class="error-message">注册失败，请稍后重试</div>';
                console.error('注册错误:', error);
            }
        });
    </script>
</body>
</html>
